<?php
namespace Orders;
class RSA
{
    public $pubkey;
    public $privkey;

    function __construct()
    {
        $this->pubkey = file_get_contents('publick_o.txt');
    }

	/**加密**/
    public function encrypt($data)
    {
        if (empty($data)) {
            return null;
        }
        $pem = "-----BEGIN PUBLIC KEY-----\n" . chunk_split($this->pubkey, 64, "\n") . "-----END PUBLIC KEY-----\n";
        $key = openssl_pkey_get_public($pem);
        $resultb = "";
        $dataArray = str_split($data, 117);
        foreach ($dataArray as $d) {
            if (openssl_public_encrypt($d, $encrypted, $key)) {
                $resultb .= $encrypted;
            } else return null;
        }
        return base64_encode($resultb);
    }


	
	/**数字签名**/
	
	 public function md5($data)
    {
    
        $xt=md5($data);		
        $xtt=urlencode(base64_encode($xt)); //数字签名
        // $resultb = "";       
        return $xtt;
    }
	
	

    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @return string content
     */
    public function http_post($url, $param)
    {
        $strPOST = '';
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
        }
        if (is_string($param)) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);
        }

        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        // echo '<pre>';
        // print_r($sContent);
        curl_close($oCurl);
        return $sContent;
    }

}